<template>
    <div class="params">
        <div>
            <el-alert title="注意: 只允许为第三级分类设置相关参数!" type="warning" style="margin: 10px 0;" show-icon>
            </el-alert>
            请选择商品分类：
            <el-cascader v-model="value" :options="options"
                :props="{ expandTrigger: 'hover', label: 'cat_name', value: 'cat_id' }"
                @change="handleChange"></el-cascader>
        </div>
        <div class="box">
            <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-button type="primary" @click="openadd" size="mini" :disabled="value != '' ? false : true">{{
                    activeName == 'many' ? '添加参数' : '添加属性' }}</el-button>
                <el-tab-pane label="动态参数" name="many">
                    <el-table :data="tableData" style="width: 100%">
                        <el-table-column type="expand">
                            <template slot-scope="props">
                                <el-tag closable :key="index" v-for="item, index in props.row.attr_vals"
                                    :disable-transitions="false" @close="handleClose(props.row, index)">
                                    {{ item }}
                                </el-tag>
                                <el-input class="input-new-tag" v-if="props.row.inputVisible" v-model="props.row.inputValue"
                                    ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm(props.row)"
                                    @blur="handleInputConfirm(props.row)">
                                </el-input>
                                <el-button v-else class="button-new-tag" size="small" @click="showInput(props.row)">+ New
                                    Tag</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column label="#" prop="index" width="20px">
                        </el-table-column>
                        <el-table-column label="参数名称" prop="attr_name">
                        </el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
                <el-tab-pane label="静态属性" name="only">
                    <el-table :data="tableData1" style="width: 100%">
                        <el-table-column type="expand">
                            <template slot-scope="props">
                                <el-tag closable :key="index" v-for="item, index in props.row.attr_vals"
                                    :disable-transitions="false" @close="handleClose(props.row, index)">
                                    {{ item }}
                                </el-tag>
                                <el-input class="input-new-tag" v-if="props.row.inputVisible" v-model="props.row.inputValue"
                                    ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm(props.row)"
                                    @blur="handleInputConfirm(props.row)">
                                </el-input>
                                <el-button v-else class="button-new-tag" size="small" @click="showInput(props.row)">+ New
                                    Tag</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column label="#" prop="index" width="20px">
                        </el-table-column>
                        <el-table-column label="参数名称" prop="attr_name">
                        </el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
            </el-tabs>
        </div>
        <!-- 对话框 -->
        <el-dialog title="添加参数" :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="参数名称" prop="attr_name">
                    <el-input v-model="ruleForm.attr_name"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>
<script>
import { getparamsList, getCategoriesList, getcateglst, addcateglist, delcateglist, editcateglst } from '../../utlis/api/shops/shopapi'
export default {
    data() {
        return {
            options: [],
            value: '',
            activeName: 'many',
            tableData: [],
            tableData1: [],
            cat_id: 1,
            inputVisible: false,
            inputValue: '',
            dynamicTags: [],
            dialogFormVisible: false,
            ruleForm: {
                attr_name: ''
            },
            rules: {
                attr_name: [
                    { required: true, message: '请输入参数名称', trigger: 'blur' }
                ],
            },
            editid: '',
            edit: []
        }
    },
    methods: {
        // 三级联动
        handleChange(value) {
            console.log(51, value);
            this.cat_id = value[value.length - 1]
            getparamsList({ cat_id: this.cat_id, sel: this.activeName }).then((res) => {
                res.data.data.forEach(item => {
                    item.attr_vals = item.attr_vals.split(',')
                    item.inputVisible = false,
                        item.inputValue = ''
                })
                console.log(59, res.data.data);
                // this.tableData = res.data.data
                if (this.activeName == 'many') {
                    this.tableData = res.data.data
                } else {
                    this.tableData1 = res.data.data
                }
            })
        },
        // tab切换
        handleClick(tab) {
            console.log(35, tab);
            this.activeName = tab._props.name
            getparamsList({ cat_id: this.cat_id, sel: this.activeName }).then((res) => {
                res.data.data.forEach(item => {
                    item.attr_vals = item.attr_vals.split(',')
                    item.inputVisible = false,
                        item.inputValue = ''
                })
                console.log(59, res.data.data);
                // this.tableData = res.data.data
                if (this.activeName == 'many') {
                    this.tableData = res.data.data
                } else {
                    this.tableData1 = res.data.data
                }
            })

        },
        // 获取三级联动列表数据
        getlist() {
            getCategoriesList().then((res) => {
                console.log(70, res);
                this.options = res.data.data
            })
        },
        // 点击修改按钮弹出模态框，数据回填
        handleEdit(row) {
            console.log(row);
            this.editid = row.cat_id
            this.dialogFormVisible = true
            this.ruleForm = JSON.parse(JSON.stringify(row))
        },
        // 删除参数
        handleDelete(row) {
            console.log(118, row);
            let obj = {
                id: row.cat_id,
                attrid: row.attr_id
            }
            delcateglist(obj).then((res) => {
                console.log(124, res);
            })
        },
        // 删除动态参数
        handleClose(row, i) {
            console.log(90, row);
            row.attr_vals.splice(i, 1)
            let obj = {
                id: row.cat_id,
                attrId: row.attr_id,
                attr_name: row.attr_name,
                attr_sel: row.attr_sel,
                attr_vals: row.attr_vals.join(',')
            }
            getcateglst(obj).then((res) => {
                console.log(71, res);
            })
        },
        // 点击弹出添加动态参数input输入框
        showInput(row) {
            row.inputVisible = true;
            this.$nextTick(_ => {
                this.$refs.saveTagInput.focus();
            });
        },
        // 添加动态参数
        handleInputConfirm(row) {
            console.log(row);
            let inputValue = row.inputValue
            if (inputValue) {
                row.attr_vals.push(inputValue);
            }
            row.inputVisible = false;
            row.inputValue = '';
            let obj = {
                id: row.cat_id,
                attrId: row.attr_id,
                attr_name: row.attr_name,
                attr_sel: row.attr_sel,
                attr_vals: row.attr_vals.join(',')
            }
            getcateglst(obj).then((res) => {
                console.log(71, res);
            })
        },
        // 点击添加弹出对话框
        openadd() {
            this.dialogFormVisible = true
        },
        // 修改，添加
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.editid) {
                        let obj = {
                            id: this.ruleForm.cat_id,
                            attrId: this.ruleForm.attr_id,
                            attr_name: this.ruleForm.attr_name,
                            attr_sel: this.ruleForm.attr_sel,
                            attr_vals: this.ruleForm.attr_vals.join(',')
                        }
                        editcateglst(obj).then((res) => {
                            console.log(res);
                            this.dialogFormVisible = false
                            getparamsList({ cat_id: this.cat_id, sel: this.activeName }).then((res) => {
                                res.data.data.forEach(item => {
                                    item.attr_vals = item.attr_vals.split(',')
                                    item.inputVisible = false
                                    item.inputValue = ''
                                })
                                console.log(59, res.data.data);
                                // this.tableData = res.data.data
                                if (this.activeName == 'many') {
                                    this.tableData = res.data.data
                                } else {
                                    this.tableData1 = res.data.data
                                }
                            })
                        })
                    } else {
                        if (this.activeName == 'many') {
                            let obj = {
                                id: this.cat_id,
                                attr_name: this.ruleForm.attr_name,
                                attr_sel: this.activeName,
                                attr_vals: this.dynamicValues
                            }
                            addcateglist(obj).then((res) => {
                                console.log(res);
                                this.tableData.push(this.ruleForm)
                                this.dialogFormVisible = false
                                this.ruleForm = {}
                            })
                        } else {
                            let obj = {
                                id: this.cat_id,
                                attr_name: this.ruleForm.attr_name,
                                attr_sel: this.activeName,
                                attr_vals: this.dynamicValues
                            }
                            addcateglist(obj).then((res) => {
                                console.log(res);
                                this.tableData1.push(this.ruleForm)
                                this.dialogFormVisible = false
                                this.ruleForm = {}
                            })
                        }
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
    },
    created() {
        this.getlist()
    }
}
</script>
<style lang="scss" scoped></style>